package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    /*
     *   新增地址
     * */
    @PostMapping
    public R addressBook(@RequestBody AddressBook addressBook) {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //设置当前登录用户 user_id
        addressBook.setUserId(userId);
        //保存地址
        boolean save = addressBookService.save(addressBook);
        return save ? R.success("添加地址成功") : R.error("添加地址失败");
    }

    /*
     *   设置默认地址
     * */
    @PutMapping("/default")
    public R updateDefault(@RequestBody AddressBook addressBook) {
        //update address_book set is_default = 0 where  user_id =  ?
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.set(AddressBook::getIsDefault, 0);
        //获取登录用户userId
        Long userId = BaseContext.getCurrentId();
        qw.eq(AddressBook::getUserId, userId);
        //更新设置为0
        addressBookService.update(qw);
        //先设置地址默认值为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    /*
     *    根据ID查询地址
     * */
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) { //?
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, id);
        AddressBook addressBook = addressBookService.getOne(qw);
        return addressBook != null ? R.success(addressBook) : R.error("用户不存在");
    }

    /*
     *   查询默认地址
     * */
    @GetMapping("/default")
    public R selectDefault() {
        //获取登录用户名user_id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, userId);
        qw.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(qw);
        return addressBook != null ? R.success(addressBook) : R.error("用户不存在");
    }

    /*
     *   查询指定用户的全部地址   select * from address_book where user_id = ?
     * */
    @GetMapping("/list")
    public R selectAllAddress() {
        //先获取用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }

    /*
     *   删除地址 可能是多个
     * */
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids) {
        //默认地址不能删除？
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /*
     *   修改地址
     * */
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook) {
        //根据登录用户名修改 先获取用户id
       /* Long userId = BaseContext.getCurrentId();
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(AddressBook::getUserId,userId);
        lqw.eq(AddressBook::getConsignee,addressBook.getConsignee());*/
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
}